package com.lw.common.webview;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebView;
import android.widget.ArrayAdapter;

import com.just.agentweb.AgentWebUIControllerImplBase;
import com.lw.common.R;

/**
 * 如果你需要修改某一个AgentWeb 内部的某一个弹窗 ，请看下面的例子
 * 注意写法一定要参照 DefaultUIController 的写法 ，因为UI自由定制，但是回调的方式是固定的，并且一定要回调。
 */
public class UIController extends AgentWebUIControllerImplBase {

    private final Activity mActivity;

    public UIController(Activity activity) {
        this.mActivity = activity;
    }

    @Override
    public void onShowMessage(String message, String from) {
        super.onShowMessage(message, from);
        Log.i(TAG, "message:" + message);
    }

//    @Override
//    public void onSelectItemsPrompt(WebView view, String url, String[] items, Handler.Callback callback) {
//       super.onSelectItemsPrompt(view,url,items,callback); // 使用默认的UI
//    }


    /**
     * 修改文件选择的弹窗
     */
    @Override
    public void onSelectItemsPrompt(WebView view, String mUrl, String[] ways, final Handler.Callback callback) {
        ArrayAdapter<CharSequence> adapter = new ArrayAdapter<>(
                mActivity, R.layout.your_custom_view, ways);
        AlertDialog alertDialog = new AlertDialog.Builder(mActivity).setSingleChoiceItems(adapter, -1, (dialog, which) -> {
            dialog.dismiss();
            if (callback != null) {
                Message mMessage = Message.obtain();
                mMessage.what = which;  //mMessage.what 必须等于ways的index
                callback.handleMessage(mMessage); //最后callback一定要回调
            }
        }).setOnCancelListener(dialog -> {
            dialog.dismiss();
            if (callback != null) {
                callback.handleMessage(Message.obtain(null, -1)); //-1表示取消  //最后callback一定要回调
            }
        }).create();
        alertDialog.show();
        Window window = alertDialog.getWindow();
        WindowManager.LayoutParams wlp = window.getAttributes();
        wlp.gravity = Gravity.BOTTOM;
        window.setAttributes(wlp);
    }
}
